/*
@Copyright:LintCode
@Author:   tjyemail
@Problem:  http://www.lintcode.com/problem/count-and-say
@Language: C++
@Datetime: 16-02-09 04:47
*/

class Solution {
public:
    /**
     * @param n the nth
     * @return the nth sequence
     */
    string countAndSay(int n) {
        // Write your code here
        char str[10000]="1",temp[10000]="",num[32];
        if (n<1)
            return "";
        if (n==1)
            return str;
        while(--n){
            int i,l;
			char c=str[0];
            for(i=1,l=1;i<strlen(str);++i){
                if(str[i]!=c){
                    sprintf(num,"%d",l);
                    strcat(temp,num);
                    sprintf(num,"%c",c);
                    strcat(temp,num);
                    c=str[i];
                    l=1;
                }
                else
                    l++;
            }
            sprintf(num,"%d",l);
            strcat(temp,num);
            sprintf(num,"%c",c);
            strcat(temp,num);
            
            strcpy(str,temp);
            strcpy(temp,"");
        }
        return str;
    }
};